package com.ruicar.afs.cloud.contract.gateway.filter;

import com.ruicar.afs.cloud.contract.gateway.processor.ProcessorHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.GenericFilterBean;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * The type ExchangeDataFilter
 * <p>Description: </p>
 *
 * @author Fzero
 * @version 1.0
 * @date Created on 2020.07.20 21:17:41
 * @since 1.0
 */
@Slf4j
@Component
@Order(Ordered.HIGHEST_PRECEDENCE+1)
public class ExchangeDataFilter extends GenericFilterBean {
    /**
     * Do filter *
     *
     * @param request  request
     * @param response response
     * @param chain    chain
     * @throws IOException      io exception
     * @throws ServletException servlet exception
     */
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest)request;
        if(httpServletRequest.getRequestURI().replaceFirst("/","").replaceAll("\\\\","").startsWith("config")){
            chain.doFilter(request,response);
        }else{
            ProcessorHelper.handler((HttpServletRequest) request,(HttpServletResponse) response);
        }
    }
}
